package com.ruoyi.web.controller.durian;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.durian.domain.dto.SaveOrderDto;
import com.ruoyi.durian.domain.dto.SearchHistoryDto;
import com.ruoyi.durian.service.LlMapService;
import com.ruoyi.durian.service.LlOrderService;
import com.ruoyi.durian.service.LlSearchHistoryService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 打车控制器
 */
@RestController
@RequestMapping("/taxi")
public class TaxiController extends BaseController {

    @Resource
    private LlSearchHistoryService llSearchHistoryService;

    @Resource
    private LlOrderService llOrderService;

    @Resource
    private LlMapService llMapService;

    /**
     * 乘客发起订单
     *
     * @return
     */
    @PostMapping("/saveOrder")
    public AjaxResult saveOrder(@RequestBody SaveOrderDto saveOrderDto) {
        return llOrderService.saveOrder(saveOrderDto);
    }

    /**
     * 历史搜索记录
     *
     * @return
     */
    @GetMapping("/searchHistory/{name}")
    public TableDataInfo searchHistory(@PathVariable("name") String name) {
        startPage();
        if (StringUtils.isNotEmpty(name) && !name.equals("undefined")) {
            return getDataTable(llSearchHistoryService.selectsearchHistoryByAddressName(name));
        }
        return getDataTable(llSearchHistoryService.searchHistory());
    }

    /**
     * 添加历史搜索记录
     *
     * @return
     */
    @PostMapping("/addHistory")
    public AjaxResult addHistory(@RequestBody SearchHistoryDto searchHistoryDto) {
        return llSearchHistoryService.addHistory(searchHistoryDto);
    }

    /**
     * 删除历史记录
     *
     * @return
     */
    @DeleteMapping("/deleteHistory")
    public AjaxResult deleteHistory() {
        return llSearchHistoryService.deleteHistory();
    }

    /**
     * 搜索记录
     * @param name
     * @return
     */
    @PostMapping("/search/like")
    public TableDataInfo searchLike(@RequestParam("name") String name) {
        startPage();
        return getDataTable(llSearchHistoryService.searchLike(name));
    }

    /**
     * 搜索地图的详情
     * @param id
     * @return
     */
    @GetMapping("/search/search/{id}")
    public AjaxResult search(@PathVariable("id") Integer id) {
        return llMapService.search(id);
    }

    /**
     * 乘客取消订单
     *
     * @param orderId  订单id
     * @return
     */
    @GetMapping("/close/order/{orderId}")
    public AjaxResult closeOrder(@PathVariable("orderId") Integer orderId) {
        return llOrderService.closeOrder(orderId);
    }

}
